package com.luo.chengrui.module.sa.controller.admin.datapermission;

import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionDefineDataDO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionDefineDO;
import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionDefineConvert;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionDefineService;

@Tag(name = "管理后台 - 用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。")
@RestController
@RequestMapping("/sa/data-permission-define")
@Validated
public class DataPermissionDefineController {

    @Resource
    private DataPermissionDefineService dataPermissionDefineService;

    @PostMapping("/create")
    @Operation(summary = "创建用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:create')")
    public CommonResult<String> createDataPermissionDefine(@Valid @RequestBody DataPermissionDefineCreateReqVO createReqVO) {
        return success(dataPermissionDefineService.createDataPermissionDefine(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:update')")
    public CommonResult<Boolean> updateDataPermissionDefine(@Valid @RequestBody DataPermissionDefineUpdateReqVO updateReqVO) {
        dataPermissionDefineService.updateDataPermissionDefine(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:delete')")
    public CommonResult<Boolean> deleteDataPermissionDefine(@RequestParam("id") String id) {
        dataPermissionDefineService.deleteDataPermissionDefine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:query')")
    public CommonResult<DataPermissionDefineRespVO> getDataPermissionDefine(@RequestParam("id") String id) {
        DataPermissionDefineDO dataPermissionDefine = dataPermissionDefineService.getDataPermissionDefine(id);
        return success(DataPermissionDefineConvert.INSTANCE.convert(dataPermissionDefine));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:query')")
    public CommonResult<List<DataPermissionDefineRespVO>> getDataPermissionDefineList(@RequestParam("ids") Collection<String> ids) {
        List<DataPermissionDefineDO> list = dataPermissionDefineService.getDataPermissionDefineList(ids);
        return success(DataPermissionDefineConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/all")
    @Operation(summary = "获得用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:query')")
    public CommonResult<List<DataPermissionDefineRespVO>> getDataPermissionDefineAllList() {
        List<DataPermissionDefineDO> list = dataPermissionDefineService.getDataPermissionDefineAllList();
        return success(DataPermissionDefineConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/allData")
    @Operation(summary = "获得控制权限的 数据。")
    @Parameter(name = "defineId", description = "权限表定义id", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:query')")
    public CommonResult<List<DataPermissionDefineDataRespVO>> getDataPermissionDefineAllData(@RequestParam("defineId") String defineId) {
        List<DataPermissionDefineDataDO> list = dataPermissionDefineService.getDataPermissionDefineAllData(defineId);
        return success(DataPermissionDefineConvert.INSTANCE.convertDataList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。分页")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:query')")
    public CommonResult<PageResult<DataPermissionDefineRespVO>> getDataPermissionDefinePage(@Valid DataPermissionDefinePageReqVO pageVO) {
        PageResult<DataPermissionDefineDO> pageResult = dataPermissionDefineService.getDataPermissionDefinePage(pageVO);
        return success(DataPermissionDefineConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。 Excel")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-define:export')")
    @OperateLog(type = EXPORT)
    public void exportDataPermissionDefineExcel(@Valid DataPermissionDefineExportReqVO exportReqVO,
                                                HttpServletResponse response) throws IOException {
        List<DataPermissionDefineDO> list = dataPermissionDefineService.getDataPermissionDefineList(exportReqVO);
        // 导出 Excel
        List<DataPermissionDefineExcelVO> datas = DataPermissionDefineConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用来定义哪些业务表用来做权限控制，如部门表、费用科目、项目表。一般用来控制权限的都是基础数据表。.xls", "数据", DataPermissionDefineExcelVO.class, datas);
    }

}
